package com.example.Sender;

import com.example.Config.RabbitMQConfig;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Description 生产者（发送消息）
 * @Author IT小辉同学
 * @Date 2023/06/16
 */
@Service

public class MessageSender {
    @Autowired

    private RabbitTemplate rabbitTemplate;

    /**
     * @param message 消息
     * @Description 发送消息
     * @Author IT小辉同学
     * @Date 2023/06/16
     */
    /**
     * 这是普通消息
     */
//    public void sendMessage(String message) {
//        System.out.println("发送祝福：" + message);
//        rabbitTemplate.convertAndSend("simple.hello", message);
//
//    }

    /**
     * 广播
     */
//    public void sendMessage(String message) {
//        System.out.println( message);
//        rabbitTemplate.convertAndSend("fanout_exchange","", message);
//
//    }
    /**
     * 订阅模式、发布模式
     */

//    public void sendMessage(String message) {
//        System.out.println( message);
//        rabbitTemplate.convertAndSend("direct_exchange","queue_route01", message);
//        rabbitTemplate.convertAndSend("direct_exchange","queue_route02", message);
//
//    }

    /**
     * 路由模式
     */
//    @Autowired
//    private AmqpTemplate amqpTemplate;
//
//    public void send(String message, String routingKey) {
//        amqpTemplate.convertAndSend(RabbitMQConfig.EXCHANGE_NAME, routingKey, message);
//        System.out.println("Sent message: " + message + ", routing key: " + routingKey);
//    }
/**
 * 主题模式
 */
@Autowired
private AmqpTemplate amqpTemplate;

    public void send(String message, String routingKey) {
        amqpTemplate.convertAndSend(RabbitMQConfig.EXCHANGE_NAME, routingKey, message);
        System.out.println("Sent message: " + message + ", routing key: " + routingKey);
    }

}





